package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.PointsAcquireBillDtoMapper;
import com.starlink.model.domain.PointsAcquireBillDO;
import com.starlink.mapper.PointsAcquireBillMapper;
import com.starlink.model.request.PointsAcquireBillRequest;
import com.starlink.service.PointsAcquireBillService;
import com.starlink.model.util.PointsAcquireBillConvert;
import com.starlink.model.vo.PointsAcquireBillVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class PointsAcquireBillServiceImpl extends ServiceImpl<PointsAcquireBillMapper, PointsAcquireBillDO> implements PointsAcquireBillService {
    @Autowired
    private PointsAcquireBillMapper pointsAcquireBillMapper;

    @Override
    public PointsAcquireBillVO getByKey(long id) {
        PointsAcquireBillVO pointsAcquireBillVO = PointsAcquireBillDtoMapper.INSTANCE.do2VO(getById(id));
        return pointsAcquireBillVO;
    }

    @Override
    public PageInfo<PointsAcquireBillVO> pageQuery(Pageable pageable,PointsAcquireBillRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<PointsAcquireBillDO> list = pointsAcquireBillMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<PointsAcquireBillVO> pointsAcquireBillVOList = PointsAcquireBillConvert.buildPointsAcquireBillVoList(list);
        return new PageInfo(pointsAcquireBillVOList);
    }

    @Override
    public List<PointsAcquireBillVO> listQuery(PointsAcquireBillRequest request) {
        List<PointsAcquireBillDO> list = pointsAcquireBillMapper.selectDOList(PointsAcquireBillDtoMapper.INSTANCE.request2DO(request));
        List<PointsAcquireBillVO> pointsAcquireBillVOS = PointsAcquireBillConvert.buildPointsAcquireBillVoList(list);
        return pointsAcquireBillVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(PointsAcquireBillRequest request) {
        request.setId(getId());
        save(PointsAcquireBillDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(PointsAcquireBillRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(PointsAcquireBillDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        pointsAcquireBillMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
